from peewee import *
from src.db import BaseModel, db


class Category(BaseModel):
    cid = AutoField()
    name = CharField()
    parent_cid = IntegerField(null=True)
    translated_name = TextField()

    class Meta:
        table_name = 'category'


class Entry(BaseModel):
    cid = ForeignKeyField(column_name='cid', field='cid', model=Category)
    eid = AutoField()

    class Meta:
        table_name = 'entry'


class EntryContent(BaseModel):
    content = TextField(index=True)
    created = IntegerField()
    ecid = AutoField()
    eid = ForeignKeyField(column_name='eid', field='eid', model=Entry)
    lang = CharField(constraints=[SQL("DEFAULT 'zh-CN'")], index=True)
    title = TextField(index=True)
    updated = IntegerField()

    class Meta:
        table_name = 'entry_content'


class History(BaseModel):
    content = TextField()
    ecid = ForeignKeyField(column_name='ecid', field='ecid', model=EntryContent)
    hid = AutoField()
    title = TextField()
    updated = IntegerField()
    user = TextField(null=True)

    class Meta:
        table_name = 'history'
